{extend name="adminblue/base" /}
{block name="resources"/}
<style type="text/css">
.input-error{font-size: 12px;color: red;line-height: 32px;display: none;}
.table-class{text-align: center;}
label{font-weight: 100;}
label input[type='radio']{margin-top: -1px;margin-right: 3px;}
.num{width:40px;text-align:center;}
.tal{text-align:left;}
#addEditArticleClass .error{margin-bottom:-6px;}
.ns-main{margin-top:0px;}
</style>
{/block}
{block name="main"}
<div class="options-btn">
	<button class="btn-common" onclick="addUpdateArticleClassShow(0);">添加分类</button>
</div>
<div class="mod-table">
	{notempty name="$list"}
	<table class="table-class">
		<colgroup>
			<col style="width: 2%">
			<col style="width: 84%;">
			<col style="width: 4%;">
			<col style="width: 10%;">
		</colgroup>
		<thead>
			<tr>
				<th></th>
				<th align="left">分类名称</th>
				<th>排序</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
			{foreach name="list" item="v1"}
			<tr class="pid_0" style="height: 30px;">
				<td>
					{if condition="$v1['child_list'] neq array()"}
					<a href="javascript:;" onclick="tab_switch({$v1['class_id']})" class="tab_jia_{$v1['class_id']}" style="display: block;">[+]</a>
					<a href="javascript:;" onclick="tab_switch({$v1['class_id']})" class="tab_jian_{$v1['class_id']}" style="display: none;">[-]</a>
					{/if}
				</td>
				
				<td style="text-align: left;"><input type="text" class="input-common" fieldid="{$v1['class_id']}" fieldname="name" value="{$v1['name']}"></td>
				<td>
					<input type="number" class="input-common input-common-sort" min="0" onkeyup="value=value.replace(/[^\d+(\.\d+)?]/g,'')" fieldid="{$v1['class_id']}" fieldname="sort" value="{$v1['sort']}" >
				</td>
				<td style="text-align: center;">
					<a href="javascript:void(0);" onclick="addUpdateArticleClassShow({$v1['class_id']});">修改</a>
					<a href="javascript:void(0);" onclick="deleteArticleClass({$v1['class_id']})">删除</a>
				</td>
			</tr>
			{foreach name="v1['child_list']" item="v2"}
			<tr class="pid_{$v1['class_id']}" style="height: 30px; display: none;">
				<td></td>
				
				<td style="text-align: left;">
					<span style="color: #ccc;">|——</span>
					<input type="text" class="input-common" fieldid="{$v2['class_id']}" fieldname="name" value="{$v2['name']}">
				</td>
				<td>
					<input type="number" class="input-common input-common-sort" min="0" onkeyup="value=value.replace(/[^\d+(\.\d+)?]/g,'')" fieldid="{$v2['class_id']}" fieldname="sort" value="{$v2['sort']}" >
				</td>
				<td style="text-align: center;">
					<a href="javascript:void(0);" onclick="addUpdateArticleClassShow({$v2['class_id']});">修改</a>
					<a href="javascript:void(0);" onclick="deleteArticleClass({$v2['class_id']})">删除</a>
				</td>
			</tr>
			{/foreach}
		{/foreach}
		</tbody>
	</table>
	{else/}
	<div class="tip-box">当前系统还没有文章分类，<a href="javascript:addUpdateArticleClassShow(0);">马上添加</a></div>
	{/notempty}
	
	<!--添加分类-->
	<div id="addEditArticleClass" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" >
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel" class="box-title">添加分类</h3>
		</div>
		<div class="modal-body">
			<div class="form-horizontal">
				
				<div class="control-group">
					<label class="control-label" for="article_class_name"><span class="color-red">*</span>分类名称：</label>
					<div class="controls">
						<input type="text" id="article_class_name" placeholder="请输入分类名称" class="input-common"/>
						<p class="error">请输入分类名称</p>
					</div>
				</div>
				
				<div class="control-group">
					<label class="control-label" for="article_class_name"><span class="color-red">*</span>上级分类：</label>
					<div class="controls">
						<select id="pid" name="account" class="select-common">
							<option value="0">顶级分类</option>
							{volist name="list" id="vo"}
							<option value="{$vo.class_id}" pid="{$vo.pid}">&nbsp;&nbsp;&nbsp;{$vo.name}</option>
							{/volist}
						</select>
					</div>
				</div>
				
				<div class="control-group">
					<label class="control-label" for="sort">排序：</label>
					<div class="controls">
						<input onkeyup="value=value.replace(/[^\d+(\.\d+)?]/g,'')" type="number" name="sort" id="sort" value="0" class="input-common harf"/>
						<p class="error">请输入排序</p>
					</div>
				</div>
			</div>
		</div>
		<div class="modal-footer">
			<button class="btn-common btn-big" id="addBtn" onClick="save()">保存</button>
			<button class="btn-common-cancle btn-big" data-dismiss="modal">关闭</button>
		</div>
	</div>
	<input type="hidden" name="class_id" id="class_id" />
</div>
{/block}
{block name="script"}
<script type="text/javascript">
$(function(){
	//弹框添加关闭
	$('[aria-hidden="true"]').click(function(){
		$(this).parents('div[role="dialog"]').hide();
	})
})
//添加修改弹框显示
function addUpdateArticleClassShow(class_id){
	$('#article_class_name_Prompt').hide();
	$('#class_id').val(class_id);
	if(class_id>0){
		$(".box-title").text("修改分类");
		$.ajax({
			type:'post',
			url:"{:__URL('ADMIN_MAIN/cms/articleclassinfo')}",
			data:{'class_id':class_id},
			success:function(data){
				$('#article_class_name').val(data['name']);
				$('#sort').val(data['sort']);
				$("#addEditArticleClass").modal("show");
				if(data['pid'] == 0){
					$("#pid option:gt(0)").hide();
				}else{
					$("#pid option:gt(0)").show();
				}
				$("#pid").val(data['pid']);
			}
		});
	}else{
		$(".box-title").text("添加分类");
		$('#article_class_name').val('');
		$('#sort').val('0');
		$("#addEditArticleClass").modal("show");
		$("#pid").val(0);
		$("#pid option:gt(0)").show();
	}
}

//添加修改的保存
function save(){
	var class_id=$('#class_id').val();
	var name=$('#article_class_name').val();
	var sort=$('#sort').val();
	var pid = $("#pid").val();
	if(name==''){
		$('#article_class_name').focus().next().show();
		return false;
	}else{
		$(".error").hide();
	}
	if(sort==''){
		$('#sort').focus().next().show();
		return false;
	}else{
		$(".error").hide();
	}
	$.ajax({
		type:'post',
		url:"{:__URL('ADMIN_MAIN/cms/addupdatearitcleclass')}",
		data:{
			'class_id':class_id,
			'name':name,
			'sort':sort,
			"pid":pid
		},
		success:function(res){
			$("#addEditArticleClass").modal("hide");
			if(res['code']>0){
				showTip(res['message'],'success');
				location.href = "{:__URL('ADMIN_MAIN/cms/articleclasslist')}";
			}else{
				showTip(res['message'],'error');
			}
		}
	});
}

//删除分类
function deleteArticleClass(class_id){
	$( "#dialog" ).dialog({
		buttons: {
			"确定": function() {
				$(this).dialog('close');
				$.ajax({
					type : "post",
					url : "{:__URL('ADMIN_MAIN/cms/deletearticleclass')}",
					dataType : "json",
					data : { "class_id" : class_id.toString() },
					success : function(res) {
						if(res['code']>0){
							showTip(res['message'],'success');
							location.href = "{:__URL('ADMIN_MAIN/cms/articleclasslist')}";
						}else{
							showTip(res['message'],'error');
						}
					}
				})
			},
			"取消,#f5f5f5,#666": function() {
				$(this).dialog('close');
			}
		},
		contentText:"是否删除分类？",
	});
}

/*修改排序*/
function load(event){
	var fieldid = $(event).attr('fieldid');
	var sort = $(event).val();
	$.ajax({
		type:"post",
		url:"{:__URL('ADMIN_MAIN/Cms/modifyField')}",
		data:{'fieldid':fieldid,'sort':sort},
		success: function (data) {
			if(data['code'] <= 0){
				showMessage('error', data["message"]);
			}
		}
	}); 
}

//展开树形数
function tab_switch(class_id){
	if($(".pid_"+class_id).css('display') != 'none'){
		$(".tab_jian_"+class_id).hide();
		$(".tab_jia_"+class_id).show();
		$(".pid_"+class_id).hide(300);
	}else{
		$(".tab_jian_"+class_id).show();
		$(".tab_jia_"+class_id).hide();
		$(".pid_"+class_id).show(500);
	}
}
$(".table-class input").change(function(){
	var fieldid = $(this).attr('fieldid');
	var fieldname = $(this).attr('fieldname');
	var fieldvalue = $(this).val();
	$.ajax({
		type:"post",
		url:"{:__URL('ADMIN_MAIN/cms/cmsfield')}",
		data:{'fieldid':fieldid,'fieldname':fieldname,'fieldvalue':fieldvalue},
		success: function (data) {
			if(data.code>0){
				showTip(data['message'],"success");
			}else{
				showTip(data['message'],"error");
			}
		}
	});
});
</script>
{/block}